import config from '@/config.js';
const commoneUrl = config.apiUrl;

//post请求封装
function postRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: 'POST',
			header: {
				'content-type': 'application/json',
			},
			success: function(res) {
				resolve(res);
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}

//get请求封装
function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "GET",
			dataType: 'json',
			success: function(res) {
				resolve(res);
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}
//put请求封装
function putRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "PUT",
			dataType: 'json',
			header: {
				'content-type': 'application/json',
			},
			success: function(res) {
				resolve(res);
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}
//del请求封装
function delRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "DELETE",
			dataType: 'json',
			header: {
				'content-type': 'application/json',
			},
			success: function(res) {
				resolve(res);
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}
module.exports = {
	post: postRequest,
	get: getRequest,
	put: putRequest,
	del: delRequest,
	ip: commoneUrl,
};